package com.task.src.service.Impl;

import com.task.src.service.EmailService;
import com.task.src.util.Verification_code;
import lombok.Value;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Service
public class EmailServiceImpl implements EmailService {

    @Resource
    private JavaMailSender javaMailSender;

    @Resource
    private HttpSession session;

    Verification_code verificationCode=new Verification_code();
    String code=verificationCode.code();

    @Override
    public boolean sendSimpleEmail(String e) {
        try {
            session.setAttribute("code",code);
            SimpleMailMessage sm=new SimpleMailMessage();
            sm.setFrom("gudupeibanzhe@qq.com");
            sm.setTo(e);
            sm.setSubject("欢迎注册我们平台");
            sm.setText("您的验证码为:"+code);
            javaMailSender.send(sm);
            return true;
        } catch (MailException ec) {
            ec.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean sendHtmlEmail(String e) {
        MimeMessage message=javaMailSender.createMimeMessage();
        MimeMessageHelper messageHelper;
        try {
            session.setAttribute("code",code);
            messageHelper=new MimeMessageHelper(message,true);
            messageHelper.setFrom("gudupeibanzhe@qq.com");
            messageHelper.setTo(e);
            message.setSubject("欢迎注册我们平台");
            messageHelper.setText("您的验证码为:"+code,true);
            javaMailSender.send(message);
            return true;
        } catch (MessagingException ec) {
            ec.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean sendAttachmentEmail(String e) {
        return false;
    }


}
